<?php
require_once 'config.php';

header('Content-Type: application/json');
error_reporting(E_ALL);
ini_set('display_errors', 1);

try {
    // 检查数据库连接
    $conn = connectDB();
    
    // 检查必要的表是否存在
    $required_tables = ['configs', 'message_templates', 'notifications'];
    $missing_tables = [];
    
    foreach ($required_tables as $table) {
        $result = $conn->query("SHOW TABLES LIKE '$table'");
        if ($result->num_rows === 0) {
            $missing_tables[] = $table;
        }
    }
    
    if (!empty($missing_tables)) {
        throw new Exception('缺少必要的表: ' . implode(', ', $missing_tables));
    }
    
    // 检查系统配置
    $config = getSystemConfig();
    
    // 检查消息模板
    $templates = getMessageTemplates();
    if (empty($templates)) {
        throw new Exception('未找到消息模板');
    }
    
    $conn->close();
    
    echo json_encode([
        'success' => true,
        'message' => '数据库连接正常，所有表和配置已就绪'
    ]);
    
} catch (Exception $e) {
    http_response_code(500);
    echo json_encode([
        'success' => false,
        'error' => $e->getMessage()
    ]);
}